package cn.ljl.basic.controller;

import cn.ljl.basic.dto.CodeDto;
import cn.ljl.basic.service.IVerifyCodeService;
import cn.ljl.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.nio.BufferOverflowException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
        private IVerifyCodeService verifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody CodeDto dto){
        try {
            verifyCodeService.getPhoneCode(dto);
            return AjaxResult.me();
        } catch (BufferOverflowException e){
            return new AjaxResult(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody CodeDto dto){
        try {
            verifyCodeService.getBinderSmsCode(dto);
            return AjaxResult.me();
        } catch (BufferOverflowException e){
            return new AjaxResult(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
}
